import java.util.Calendar;
import java.util.Enumeration;
import java.util.LinkedList;

/*
 * This class is used to perform the following functions
 * 
 * 		1. Estimate financial progress
 * 			1. Expenditure estimate
 * 			2. Savings estimate
 * 
 * Probable implementation would incorporate having a list of all expenditures, which would also include list of repeating items
 * being duplicated.
 * 
 * ===== NOTE: The progress is only calculated for the current calendar year.
 */
public class FinancialProgress implements Runnable
{
	private volatile ITable itemTable = null;
	private volatile IDataHandler dataHandler = null;
	
	private double estimateExpenditure = 0.0;
	private double estimateSaving = 0.0;
	private double totalIncome = 0.0;

	private int lastItemCount = 0;
	
	private LinkedList<IFinancial_Item> recordList = null;
	
	public FinancialProgress(ITable iTable, IDataHandler dHandler)
	{
		this.itemTable = iTable;
		this.dataHandler = dHandler;
		
		recordList = new LinkedList<IFinancial_Item>();
		
		calculateExpenditure();
	}

	public void run() 
	{
		while(true)
		{
			// Keep updating the estimates
			// Estimation done for the current year only
			
			if(itemTable.getRecordCount() != lastItemCount)
			{
				lastItemCount = itemTable.getRecordCount();
				
				Enumeration<IFinancial_Item> records = itemTable.getEnumerator();
				
				while(records.hasMoreElements())
				{
					IFinancial_Item currentItem = records.nextElement();
					
					recordList.add(currentItem);
					
					if(currentItem.getRecurrence() == true)
					{
						// According to the recurrence nature add the modified item into the record list
						
						/*
						 * The following general rules apply:
						 * 		1. If the estimated date of occurrence is today or in the past, 
						 * 			then add it to the excel sheet and the item table
						 * 		2. If estimated date is in the future, then only add it to the record list if the date is within
						 * 			the current year
						 */
						
						String recNature = currentItem.getRecurrenceNature();
						Calendar estimateDate = Calendar.getInstance();
						
						estimateDate.setTime(currentItem.getDate());
						
						if(recNature.equals("Yearly"))
						{							
							IFinancial_Item newItem = null;
							int count = 1;
							
							estimateDate.add(Calendar.YEAR, count);
							
							while(!estimateDate.after(Calendar.getInstance()))
							{
								newItem = new Financial_Item(currentItem.getItemName() + "_" + count,
																currentItem.getItemDesc() + "_" + count,
																currentItem.getItemValue(),
																false,
																null,
																ItemPriority.valueOf(currentItem.getItemPriority()));
																
								newItem.setDay_Of_Month(estimateDate.get(Calendar.DAY_OF_MONTH));
								newItem.setMonth(estimateDate.get(Calendar.MONTH));
								newItem.setYear(estimateDate.get(Calendar.YEAR));
								
								recordList.add(newItem);
								dataHandler.addFinancialItem(newItem);
								
								//count++;
								estimateDate.add(Calendar.YEAR, ++count);
							}
							
							// Add the last future expense in the records list if it is within the current year
							
							if(newItem != null)
							{
								if(estimateDate.get(Calendar.YEAR) == Calendar.getInstance().get(Calendar.YEAR))
								{
									// Create new item and add it
									
									newItem = new Financial_Item(currentItem.getItemName() + "_" + count,
																	currentItem.getItemDesc() + "_" + count,
																	currentItem.getItemValue(),
																	false,
																	null,
																	ItemPriority.valueOf(currentItem.getItemPriority()));
							
									newItem.setDay_Of_Month(estimateDate.get(Calendar.DAY_OF_MONTH));
									newItem.setMonth(estimateDate.get(Calendar.MONTH));
									newItem.setYear(estimateDate.get(Calendar.YEAR));
									
									recordList.add(newItem);
								}
							}
						}
					}
					
				}
			}
		}
	}
	
	private void calculateExpenditure()
	{
		// Seperate thread function to calculate the expenditure according to the record list.
	}
	
	public double getEstimatedExpenditure()
	{
		return this.estimateExpenditure;
	}
	
	public double getEstimatedSavings()
	{
		return this.estimateSaving;
	}
	
	public double getTotalIncome()
	{
		return this.totalIncome;
	}
}
